package cn.itcast.netty.c3;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TestNettyFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();

        EventLoop eventLoop = group.next();

        Future<Object> future = eventLoop.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                log.debug("执行计算");
                TimeUnit.SECONDS.sleep(1);
                return 70;
            }
        });

//        log.debug("等待结果");
//        log.debug("结果是:{}",future.get());

        log.debug("等待结果");
        future.addListener(new GenericFutureListener<Future<? super Object>>(){
            @Override
            public void operationComplete(Future<? super Object> future) throws Exception {
                log.debug("结果是:{}",future.getNow());
            }
        });
    }
}
